package com.ali.juejin.base

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

abstract class BaseModelAdapter<K:ViewDataBinding,L,M>(val mContext:Context,val mBr: Int,val model:M): RecyclerView.Adapter<BaseModelAdapter.ListHolder<K>>() {

    protected var list = ArrayList<L>()

    class ListHolder<T: ViewDataBinding>(val databind: T) : RecyclerView.ViewHolder(databind.root){
        fun getBinding():T{
            return databind
        }
    }

    // 加载数据
    open fun load(){
        list.clear()
        list.addAll(getDataList())
        notifyDataSetChanged()
    }

    // 单项布局
    abstract fun getLayout():Int

    // 获取数据列表
    abstract fun getDataList():ArrayList<L>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder<K> {
        return ListHolder(DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayout(),parent,false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ListHolder<K>, position: Int) {
        val itemData = list[position]
        val binding = holder.getBinding()
        itemViewHandler(holder,position)
        binding.setVariable(mBr,itemData)
        binding.executePendingBindings()
    }

    open fun itemViewHandler(holder: ListHolder<K>, position: Int){

    }

}